package com.wisdytech.linkdcs.system.action;

import java.util.Date;
import java.util.List;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.github.pagehelper.PageInfo;
import com.wisdytech.common.bean.Filter;
import com.wisdytech.common.bean.StandardResult;
import com.wisdytech.common.bean.StandardTableResult;
import com.wisdytech.common.utils.BaseUtil;
import com.wisdytech.common.utils.PojoConvertUtil;
import com.wisdytech.linkdcs.system.model.SysResource;
import com.wisdytech.linkdcs.system.service.ISystemResourceService;
import com.wisdytech.linkdcs.system.vo.ResourceVO;
import com.wisdytech.linkdcs.system.vo.TreeviewNodeVO;

/**
 * 
 * <p>
 * ClassName: SystemResourceAction
 * </p>
 * <p>
 * Description: 资源管理
 * </p>
 * <p>
 * Author: kongming
 * </p>
 * <p>
 * Date: 2017-9-13
 * </p>
 */
@Controller
@RequestMapping("/system/resource")
public class SystemResourceAction {
    
    @Autowired
    @Qualifier("systemResourceService")
    private ISystemResourceService systemResourceService;
    
    @RequestMapping("/index")
    public String resourceIndex() {
    	return "system/resource/index";
    }
    
    
    
    /**
     * <p>Description: 查询资源信息</p>
     * @param resName 资源名称
     * @param resType 资源类型
     * @param filter 分页参数
     * @return 
     */
    @RequestMapping(value = "/listSysResource/get.mvc")
    @ResponseBody
    public StandardTableResult<ResourceVO> listSysResource(String resName, String resType, Filter filter) {
        PageInfo<SysResource> sysResourcePage = systemResourceService.listSysResources(resName, resType, filter);
        //转换为前台结果集
        List<SysResource> resourceList = sysResourcePage.getList();
        
        List<ResourceVO> resourceVOList =
        		PojoConvertUtil.convertPojos(resourceList, ResourceVO.class);
        
        
        
        return new StandardTableResult<ResourceVO>(sysResourcePage.getTotal(),resourceVOList);
    }
    
    /**
     * <p>Description: 保存资源</p> 
     * @param id 资源唯一标识id
     * @return boolean值
     */
    @RequestMapping(value = "/saveSysResource/save.mvc")
    @ResponseBody
    public StandardResult saveSysResource(SysResource sysResource) {
        if (StringUtils.isBlank(sysResource.getPkId())) {
            return systemResourceService.addSysResource(sysResource);
        }else {
            return systemResourceService.updateSysResource(sysResource);
        }
    }
    
    /**
     * <p>Description: 删除资源信息</p> 
     * @param id 资源唯一标识id
     * @return boolean值
     */
    @RequestMapping(value = "/delSysResourceById/del.mvc")
    @ResponseBody
    public StandardResult delSysResourceById(String id) {
        return systemResourceService.delSysResourceById(id);
    }
    
    
    /**
     * 
     * <p>Description: 保存角色资源信息</p>
     * @param roleId 角色id
     * @param roleResource 角色列表
     * @return
     */
    @RequestMapping(value="/saveRoleResource/save.mvc")
    @ResponseBody
    public StandardResult saveRoleResource(String roleId,String roleResource){
        if(StringUtils.isBlank(roleId)){
            return StandardResult.getFailResult();
        }
        if(StringUtils.isBlank(roleResource)){
            return StandardResult.getFailResult();
        }
        
        return systemResourceService.saveSysResourceToRoleId(roleId, roleResource);

    }
    
    @RequestMapping("/getResource/get.mvc")
    @ResponseBody
    public StandardResult getResourceById(String id) {
    	if(StringUtils.isBlank(id)) {
    		return StandardResult.getFailResult("资源id为空");
    	}
    	SysResource  resource =
    			systemResourceService.getSysResourceById(id);
    	
    	return StandardResult.getSuccessResult(PojoConvertUtil.convertPojo(resource, ResourceVO.class));
    }
    
    /**
     * 
     * <p>Description: 资源属性结构</p>
     * @param parentId 父级节点id
     * @return
     */
    @RequestMapping(value="/listResource/get.mvc")
    @ResponseBody
    public StandardResult listRoleResourcs(String parentId){
        
        List<TreeviewNodeVO>  resources=
        		systemResourceService.getParentResourceTree(parentId);
        return StandardResult.getSuccessResult(resources);
    }
    
    /**
     * 
     * <p>Description: 资源属性结构</p>
     * @param parentId 父级节点id
     * @return
     */
    @RequestMapping(value="/udpateResParent/update.mvc")
    @ResponseBody
    public StandardResult updateParentNode(String childId,String parentId) {
    	
    	SysResource sysResource =new SysResource();
    	sysResource.setPkId(childId);
    	sysResource.setParentId(parentId);
    	sysResource.setUpdateUser(BaseUtil.getCurrentUser().getUsername());
    	sysResource.setUpdateDate(new Date());
    	return systemResourceService.updateSysResource(sysResource);
    }

    /**
     *
     * <p>Description: 资源属性结构</p>
     * @param resCode 父级节点id
     * @return
     */
    @RequestMapping(value="/getParentNode/get.mvc")
    @ResponseBody
    public StandardResult getParentNode(String resCode) {
        String parentNode = this.systemResourceService.getParentNode(resCode);
        System.out.println(parentNode);
        return StandardResult.getSuccessResult(parentNode);
    }
    
}
